package com.engine4cloud.tdf.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.education.convert.EduGroupConvert;
import com.engine4cloud.tdf.education.dto.create.EduGroupCreateDTO;
import com.engine4cloud.tdf.education.dto.query.EduGroupQueryDTO;
import com.engine4cloud.tdf.education.dto.result.EduGroupResultDTO;
import com.engine4cloud.tdf.education.dto.update.EduGroupUpdateDTO;
import com.engine4cloud.tdf.education.entity.EduGroupEntity;
import com.engine4cloud.tdf.education.mapper.EduGroupMapper;
import com.engine4cloud.tdf.education.service.EduGroupService;
import com.engine4cloud.tdf.education.service.EduStudentService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 学组
 *
 * @author wenjie
 * @date 2024-07-30 14:57:54
 */
@Service
public class EduGroupServiceImpl extends ServiceImpl<EduGroupMapper, EduGroupEntity> implements EduGroupService {
    @Resource
    private EduStudentService studentService;

    @Override
    public PageInfo<EduGroupResultDTO> pageList(Params pageable, EduGroupQueryDTO dto) {
        IPage<EduGroupEntity> page = super.page(Pages.page(pageable), wrapper(dto));
        return Pages.convert(page, EduGroupConvert.INSTANCE::convert);
    }

    @Override
    public EduGroupResultDTO getById(Long id) {
        return EduGroupConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(EduGroupCreateDTO dto) {
        TdfUser user = SecurityUtils.getUser();
        EduGroupEntity entity = EduGroupConvert.INSTANCE.create(dto, user.getTenantId());
        super.save(entity);
        //保存学生
        Actions.apply(CollectionUtils.isNotEmpty(dto.getStudentList()), () -> studentService.batchCreate(entity.getCode(), dto.getStudentList()));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(EduGroupUpdateDTO dto) {
        EduGroupEntity old = Optional.ofNullable(this.baseMapper.selectById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        EduGroupEntity entity = EduGroupConvert.INSTANCE.update(dto);
        super.updateById(entity);
        //保存学生
        Actions.apply(CollectionUtils.isNotEmpty(dto.getStudentList()), () -> studentService.batchUpdate(old.getCode(), dto.getStudentList()));
        return true;
    }

    @Override
    public List<EduGroupResultDTO> findListByCourseCode(String courseCode) {
        return this.baseMapper.findListByCourseCode(courseCode);
    }

    private LambdaQueryWrapper<EduGroupEntity> wrapper(EduGroupQueryDTO query) {
        LambdaQueryWrapper<EduGroupEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(EduGroupEntity::getId, v));
        Queries.accept(query.getCode(), v -> wrapper.eq(EduGroupEntity::getCode, v));
        Queries.accept(query.getName(), v -> wrapper.like(EduGroupEntity::getName, v));
        wrapper.orderByDesc(EduGroupEntity::getCreateTime);
        return wrapper;
    }
}